package com.wc.算法提高课.A第一章_动态规划.背包模型.有依赖的背包问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/9 21:38
 * @description https://www.acwing.com/problem/content/10/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static int[] h = new int[N], e = new int[N], ne = new int[N];
    // f[i][j] 表示节点i,体积为j的最大价值
    static int[][] f = new int[N][N];
    static int[] w = new int[N], v = new int[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        int root = -1;
        for (int i = 1; i <= n; i++) {
            w[i] = sc.nextInt();
            v[i] = sc.nextInt();
            int p = sc.nextInt();
            if (p == -1) root = i;
            else add(p, i);
        }
        dfs(root);
        out.println(f[root][m]);
        out.flush();
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static void dfs(int u) {
        for (int i = h[u]; i > 0; i = ne[i]) {
            int son = e[i];
            dfs(son);
            // 分组背包问题，因为给自己留下体积才能满足最大的
            for (int j = m - w[u]; j >= 0; j--) {
                // 决策，相当于给子孩子多少体积
                for (int k = 0; k <= j; k++) {
                    f[u][j] = Math.max(f[u][j], f[u][j - k] + f[son][k]);
                }
            }
        }
        // 把自己给加上
        for (int i = m; i >= w[u]; i--) f[u][i] = f[u][i - w[u]] + v[u];
        // 小于的一定是0
        for (int i = 0; i < w[u]; i++) f[u][i] = 0;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
